package com.example.demo.controller;


import com.example.demo.entity.User;
import com.example.demo.entity.vo.GroupInput;
import com.example.demo.entity.vo.GroupOutput;
import com.example.demo.entity.vo.LoginInput;
import com.example.demo.entity.vo.LoginOutput;
import com.example.demo.service.UserService;
import com.example.demo.utils.Md5Util;
import com.example.demo.utils.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chen huilong
 * @since 2022-10-04
 */
@Api(tags = "用户信息管理")
@RestController
@RequestMapping("/demo/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Response register() {
        User user = new User();
        user.setParentId(1L);
        user.setUserNumber(2222222222L);
        user.setUserName("马尔福");
        user.setPassword(Md5Util.MD5("888888"));
        user.setUserRole("学生");
        user.setEmail("1933005838@qq.com");
        user.setPhonenumber("13787654223");
        user.setGender("0");
        user.setStatus(true);
        userService.register(user);
        return Response.success();
    }


    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "使用学号密码方式登录")
    public Response login(@RequestBody LoginInput loginInput, HttpServletRequest request, HttpServletResponse response) {
        LoginOutput res = userService.login(loginInput, request, response);
        return Response.success().data("sign-in info", res);
    }

    @PostMapping("/changePassword")
    @ApiOperation(value = "修改密码")
    public Response changePassword(@RequestBody Map<String, Object> input, HttpServletRequest request) {
        userService.changePassword(input, request);
        return Response.success();
    }

    @PostMapping("/getUserInfo")
    @ApiOperation(value = "查询个人信息", notes = "")
    public Response getUserInfo(@RequestHeader("access_token") String token, HttpServletRequest request) {
        User user = userService.getUserInfo(token, request);
        return Response.success().data("user info", user);
    }

    @PostMapping("/setVerificationCode")
    @ApiOperation(value = "设置个人组队邀请验证码", notes = "")
    public Response setVerificationCode(@RequestHeader("token") String token, @RequestBody String verificationCode) {
        userService.setVerificationCode(token, verificationCode);
        return Response.success();
    }


    @PostMapping("/modifyStuInfo")
    @ApiOperation(value = "管理员修改学生信息", notes = "")
    public Response modifyStuInfo(@RequestHeader("token") String token, @RequestBody User user) {
        userService.modifyStuInfo(token, user);
        return Response.success();
    }


}

